/**
 * 统一调用接口
 */

import store from "../utils/factory";

const isDev = false;
const baseUrl = isDev ?
    "http://106.15.91.173/platform/" :
    "https://platform.tianduhotel.top/groupbuy/";
export const appid = "wx46d18594087bb677";

let loginModal = false

function request(url, data, method) {
    return new Promise((resolve, reject) => {
        let header = {};
        if (store.get("cookie")) {
            header = {
                "Content-Type": "application/json;charset=UTF-8",
                cookie: store.get("cookie"),
                terminalType: "wx",
                type: "retail"
            };
            // console.log("getCookie")
        } else {
            header = {
                "Content-Type": "application/json;charset=UTF-8",
                terminalType: "wx",
                type: "retail"
            };
            // console.log("NOTCookie")
        }
        wx.request({
            url: baseUrl + url,
            data: data || {},
            header: header,
            method: method || "GET",
            dataType: "json",
            responseType: "text",
            success: res => {
                let statusCode = res.statusCode;
                if (res && res.header && res.header["Set-Cookie"]) {
                    let header = res.header;
                    store.set("cookie", header["Set-Cookie"]);
                }
                if (statusCode == 200) {
                    let req = res.data;
                    if (req.code == 0 || !req.code) {
                        resolve(req);
                    } else {
                        wx.hideLoading();
                        if (req.code == 500) {
                            wx.showToast({
                                title: "服务器异常",
                                icon: "none",
                                duration: 2000
                            });
                        } else if (req.code == 401) {
                            lostLogin()
                        } else {
                            wx.showToast({
                                title: req.msg || "服务器异常",
                                icon: "none",
                                duration: 2000
                            });
                        }
                        reject(req);
                    }
                } else {
                    wx.hideLoading();
                    if (statusCode >= 500) {
                        wx.showToast({
                            title: "服务器异常，请稍后再试",
                            icon: "none",
                            duration: 2000
                        });
                        reject(res);
                    } else {
                        if (statusCode == 401) {
                            lostLogin();
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: "none",
                                duration: 2000
                            });
                            reject(res);
                        }
                    }
                }
            },
            fail: res => {
                wx.hideLoading();
                wx.showToast({
                    title: "网络异常，请检查网络状态",
                    icon: "none",
                    duration: 2000
                });
                reject(res);
            }
        });
    });
}

function lostLogin() {
    if (loginModal) {
        return false
    }
    loginModal = true;
    store.clear();
    logout().then(res => {
        wx.showModal({
            title: "温馨提示",
            content: "登录已过期，请重新登录",
            showCancel: false,
            complete: function (req) {
                if (req.confirm) {
                    loginModal = false;
                    wx.navigateTo({
                        url: "../autoLogin/autoLogin"
                    });
                }
            }
        });
    })
}

/**
 *
 * 微信登录
 * @method wxLogin
 * @param
 *
 */
const wxLogin = function (data) {
    let url = "wx-user/login";
    return request(url, data, "POST");
};

/**
 *
 * 获取微信用户信息
 * @method wxUserInfo
 * @param
 *
 */
const wxUserInfo = function (data) {
    let url = "wx-user/info";
    return request(url, data, "POST");
};

/**
 *
 * 推荐查询
 * @method getRecommendList
 * @param
 *
 */
const getRecommendList = function (data) {
    let url = "wx-subject/query";
    return request(url, data, "GET");
};

/**
 *
 * 获取栏目
 * @method getCategoryList
 * @param
 *
 */
const getCategoryList = function (data) {
    let url = "wx-category/query";
    return request(url, data, "GET");
};

/**
 *
 * 获取商品列表
 * @method getSubList
 * @param
 *
 */
const getSubList = function (data) {
    let url = "wx-category/getSub";
    return request(url, data, "POST");
};

/**
 *
 * banner查询
 * @method getBannerList
 * @param
 *
 */
const getBannerList = function (data) {
    let url = "wx-banner/query";
    return request(url, data, "GET");
};

/**
 *
 * 新增地址
 * @method addAdress
 * @param
 *
 */
const addAdress = function (data) {
    let url = "wx-address/add";
    return request(url, data, "POST");
};

/**
 *
 * 获取参数
 * @method getConfig
 * @param
 *
 */
const getConfig = function (data) {
    let url = "setting/get";
    return request(url, data, "POST");
};

/**
 *
 * 通知
 * @method notice
 * @param
 *
 */
const notice = function (data) {
    let url = "wx-banner/notice";
    return request(url, data, "GET");
};
/**
 *
 * 设置默认地址
 * @method setDefault
 * @param
 *
 */
const setDefault = function (data) {
    let url = "wx-address/default";
    return request(url, data, "POST");
};

/**
 *
 * 删除地址
 * @method delAdress
 * @param
 *
 */
const delAdress = function (data) {
    let url = "wx-address/delete";
    return request(url, data, "POST");
};

/**
 *
 * 列出用户地址
 * @method getAdressList
 * @param
 *
 */
const getAdressList = function (data) {
    let url = "wx-address/list";
    return request(url, data, "POST");
};

/**
 *
 * 查询地址列表
 * @method searchAdressList
 * @param
 *
 */
const searchAdressList = function (data) {
    let url = "wx-address/query";
    return request(url, data, "GET");
};

/**
 *
 * 更新地址
 * @method updateAdress
 * @param
 *
 */
const updateAdress = function (data) {
    let url = "wx-address/update";
    return request(url, data, "POST");
};

// wx-order-controller : 微信订购业务

/**
 *
 * 提交订单
 * @method addOrder
 * @param
 *
 */
const addOrder = function (data) {
    let url = "wx-order/add";
    return request(url, data, "POST");
};

/**
 *
 * 通过订单
 * @method passOrder
 * @param
 *
 */
const passOrder = function (data) {
    let url = "wx-order/pass";
    return request(url, data, "POST");
};

/**
 *
 * 支付订单
 * @method payOrder
 * @param
 *
 */
const payOrder = function (data) {
    let url = "wx-order/pay";
    return request(url, data, "POST");
};

/**
 *
 * 查询订单
 * @method queryOrder
 * @param
 *
 */
const queryOrder = function (data) {
    let url = "wx-order/query";
    return request(url, data, "GET");
};

/**
 *
 * 订单退款
 * @method refundOrder
 * @param
 *
 */
const refundOrder = function (data) {
    let url = "wx-order/refund";
    return request(url, data, "POST");
};

/**
 *
 * 取消订单
 * @method cancalOrder
 * @param
 *
 */
const cancalOrder = function (data) {
    let url = "wx-order/batchDelete";
    return request(url, data, "POST");
};

/**
 *
 * 查询订单
 * @method getOrderDetail
 * @param
 *
 */
const getOrderDetail = function (data) {
    let url = "wx-order/detail";
    return request(url, data, "POST");
};

/**
 *
 * 查询运费
 * @method getExpress
 * @param
 *
 */
const getExpress = function (data) {
    let url = "wx-order/getExpress";
    return request(url, data, "GET");
};

/**
 *
 * 热门搜索
 * @method hotSearch
 * @param
 *
 */
const hotSearch = function (data) {
    let url = "wx-recommend/hot";
    return request(url, data, "GET");
};

/**
 *
 * 搜索
 * @method searchList
 * @param
 *
 */
const searchList = function (data) {
    let url = "wx-category/search";
    return request(url, data, "POST");
};

/**
 *
 * 登出
 * @method logout
 * @param
 *
 */
const logout = function (data) {
    let url = "user/logout";
    return request(url, data, "POST");
};

/**
 *
 * 获取详情
 * @method getDetail
 * @param
 *
 */
const getDetail = function (data) {
    let url = "wx-category/detail";
    return request(url, data, "POST");
};

/**
 *
 * 添加购物车
 * @method shopAdd
 * @param
 *
 */
const shopCarAdd = function (data) {
    let url = "wx-shopping/add";
    return request(url, data, "POST");
};

/**
 *
 * 删除购物车
 * @method shopAdd
 * @param
 *
 */

const delShopCar = function (data) {
    let url = "wx-shopping/delete";
    return request(url, data, "POST");
};

/**
 *
 * 获取购物车列表
 * @method getShopCarList
 * @param
 *
 */

const getShopCarList = function (data) {
    let url = "wx-shopping/get";
    return request(url, data, "GET");
};

/**
 *
 * 更新购物车
 * @method updateShopCar
 * @param
 *
 */

const updateShopCar = function (data) {
    let url = "wx-shopping/update";
    return request(url, data, "POST");
};

/**
 *
 * 获取统计
 * @method getStatistics
 * @param
 *
 */

const getStatistics = function (data) {
    let url = "wx-order/getStatistics";
    return request(url, data, "GET");
};

/**
 *
 * 获取统计
 * @method getSummary
 * @param
 *
 */

const getSummary = function (data) {
    let url = "wx-order/calcSummary";
    return request(url, data, "POST");
};

/**
 *
 * 查询优惠券
 * @method getVoucherList
 * @param
 *
 */

const getVoucherList = function (data) {
    let url = "wx-voucher/query";
    return request(url, data, "GET");
};


/**
 *
 * 查询优惠券
 * @method getVoucherList
 * @param
 *
 */

const getVoucherNum = function (data) {
    let url = "wx-voucher/get";
    return request(url, data, "POST");
};

/**
 *
 * 查询配送地址
 * @method getListCollect
 * @param
 *
 */

const getListCollect = function (data) {
    let url = "wx-address/listCollect";
    return request(url, data, "GET");
};

/**
 *
 * addVip
 * @method addVip
 * @param
 *
 */

const addVip = function (data) {
    let url = "wx-member/add";
    return request(url, data, "POST");
};

/**
 *
 * rechargeList
 * @method rechargeList
 * @param
 *
 */

const rechargeList = function (data) {
    let url = "wx-member/rechargeList";
    return request(url, data, "POST");
};

/**
 *
 * recharge
 * @method recharge
 * @param
 *
 */

const recharge = function (data) {
    let url = "wx-member/recharge";
    return request(url, data, "POST");
};

/**
 *
 * getWallet
 * @method getWallet
 * @param
 *
 */

const getWallet = function (data) {
    let url = "wx-member/get";
    return request(url, data, "POST");
};

/**
 *
 * getComment
 * @method getComment
 * @param
 *
 */

const getComment = function (data) {
    let url = "wx-order/comment";
    return request(url, data, "POST");
};

/**
 *
 * addComment
 * @method addComment
 * @param
 *
 */

const addComment = function (data) {
    let url = "wx-order/addComment";
    return request(url, data, "POST");
};

export {
    getComment,
    addComment,
    wxLogin,
    getConfig,
    wxUserInfo,
    shopCarAdd,
    getWallet,
    rechargeList,
    recharge,
    updateShopCar,
    delShopCar,
    getShopCarList,
    getRecommendList,
    getBannerList,
    getCategoryList,
    getListCollect,
    getSubList,
    getStatistics,
    addAdress,
    setDefault,
    delAdress,
    addVip,
    getAdressList,
    searchAdressList,
    updateAdress,
    addOrder,
    refundOrder,
    queryOrder,
    payOrder,
    passOrder,
    cancalOrder,
    notice,
    hotSearch,
    searchList,
    getDetail,
    logout,
    getOrderDetail,
    getExpress,
    getVoucherList,
    getVoucherNum,
    getSummary
};